package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.front.config.idempotent.Idempotent;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.biz.BaseInfoService;
import com.sinog.front.service.user.UserFacade;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 预安置管理 - 服刑人员信息录入 - InmatesController
 * @author dh
 * @date 2020年1月15日
 */
@Slf4j
@RestController
public class InmatesController {

    @Resource
    private BaseInfoService baseInfoService;
    @Resource
    private UserFacade userService;

    /**
     * 预安置管理 - 预安置人员信息录入 - 基本信息(新增,更新)
     * @param request HttpServletRequest
     * @param data 基本信息字符串
     * @return Result
     */
    @Log(type = "新增/更新", value = "保存罪犯基本信息")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/inmates/baseinfo/save")
    public Result saveBaseInfo(HttpServletRequest request,
                               @RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        String id = JsonUtil.getValueFromJsonstrByKey(data, Constants.StrFinalValue.ID);
        // 修改
        if(StringUtils.isNotBlank(id)) {
            return baseInfoService.saveBaseInfo(data,user,Constants.NumberStr.NUM_STR_ONE);
        }
        // 新增
        return baseInfoService.saveBaseInfo(data,user,Constants.NumberStr.NUM_STR_ZERO);
    }

    /**
     * 预安置管理 - 预安置人员信息录入 - 批量导入 - 基本信息(更新) - excel
     * @param request request
     * @param data 业务数据
     * @return Result
     */
    @Log(type = "更新", value = "保存罪犯基本信息")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/inmates/baseinfo/saveexcel")
    public Result saveBaseInfoExcel(HttpServletRequest request,
                                    @RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        String idNum = JsonUtil.getValueFromJsonstrByKey(data, "idNum");
        String id = JsonUtil.getValueFromJsonstrByKey(data, Constants.StrFinalValue.ID);
        if(StringUtils.isBlank(idNum)) {
            data = JsonUtil.setObjectToJsonStr(data, "idNum","无");
        }
        if(StringUtils.isNotBlank(id)) {
            return baseInfoService.updataBaseInfoExcel(data,user);
        }
        // 不存在新增,返回空
        //todo 测试是否能直接返回result对象
        return null;
    }

    /**
     * 预安置管理 - 预安置人员信息录入 - 查询列表
     * @param pageIndex 分页起始索引
     * @param pageSize 分页条数
     * @param pName 姓名
     * @param sex 性别
     * @param nativepro 省
     * @param nativecity 市
     * @param nativecou 区县
     * @param nativevill 街道
     * @param qrbegin 刑期起日（开始日期）
     * @param qrend 刑期起日（结束日期）
     * @param zrbegin 刑期止日（开始日期）
     * @param zrend 刑期止日（结束日期）
     * @param inputSource 录入来源 01:司法所补录的解矫人员,05:司法所补录的监狱人员,3.看守所录入的服刑人员,4:接口进入的看守所人员,5:监狱录入的服刑人员,6:接口进入的监狱服刑人员,7:社矫录入的服刑人员,8:接口进入的社矫人员,9:监狱通过excel导入,10:社矫通过excel导入,11:看守所通过excel导入,30:湖南看守所个性化对接
     * @param listType 列表类型 100:默认列表    101:户籍完整人员  102:查不清户籍   103:有未成年子女  104:无照片或指纹人员    105:姓名重复人员
     * @return 预安置人员信息录入数据列表
     */
    @RequestMapping("/azbj/inmates/info/list")
    public String getInmatesList(Integer pageIndex,Integer pageSize,
                                 @RequestParam(required = false, defaultValue = "-1") String sortField,
                                 @RequestParam(required = false, defaultValue = "-1") String sortOrder,
                                 @RequestParam(required = false, defaultValue = "-1") String pName,
                                 @RequestParam(required = false, defaultValue = "-1") String sex,
                                 @RequestParam(required = false, defaultValue = "-1") String nativepro,
                                 @RequestParam(required = false, defaultValue = "-1") String nativecity,
                                 @RequestParam(required = false, defaultValue = "-1") String nativecou,
                                 @RequestParam(required = false, defaultValue = "-1") String nativevill,
                                 @RequestParam(required = false, defaultValue = "-1") String qrbegin,
                                 @RequestParam(required = false, defaultValue = "-1") String qrend,
                                 @RequestParam(required = false, defaultValue = "-1") String zrbegin,
                                 @RequestParam(required = false, defaultValue = "-1") String zrend,
                                 @RequestParam(required = false, defaultValue = "-1") String inputSource,
                                 @RequestParam(required = false, defaultValue = "-1") String listType) {
        UserDTO user = userService.getAuthentication();
        Map<String,String> map = new HashMap<>(16);
        map.put("departid",user.getDepartid());
        map.put("pName",pName);
        map.put("sex",sex);
        map.put("nativepro",nativepro);
        map.put("nativecity",nativecity);
        map.put("nativecou",nativecou);
        map.put("nativevill",nativevill);
        map.put("qrbegin",qrbegin);
        map.put("qrend",qrend);
        map.put("zrbegin",zrbegin);
        map.put("zrend",zrend);
        map.put("inputSource",inputSource);
        map.put("sortField",sortField);
        map.put("sortOrder",sortOrder);
        map.put("listType",listType);
        String mapText = JsonUtil.toText(map);
        return baseInfoService.getInmatesList(pageIndex,pageSize,mapText);
    }

    /**
     * 预安置管理 - 预安置人员信息录入 - 批量导入 - 查询列表
     * @param pageIndex 分页起始索引
     * @param pageSize 分页条数
     * @param querykey querykey
     * @param pName 姓名
     * @param idNum idNum
     * @param sex 性别
     * @param nativepro 省
     * @param nativecity 市
     * @param nativecou 区县
     * @param nativevill 区县
     * @param qrbegin 刑期起日（开始日期）
     * @param qrend 刑期起日（结束日期）
     * @param zrbegin 刑期止日（开始日期）
     * @param zrend 刑期止日（结束日期）
     * @param oldKey 是否旧数据
     * @param uuid 批量导入批次
     * @param successFlag 是否解析成功
     * @param date 导入日期
     * @return String
     */
    @RequestMapping("/azbj/inmates/info/listExcel")
    public String getInmatesListExcel(Integer pageIndex,Integer pageSize,
                                      @RequestParam(required = false, defaultValue = "-1") String querykey,
                                      @RequestParam(required = false, defaultValue = "-1") String pName,
                                      @RequestParam(required = false, defaultValue = "-1") String idNum,
                                      @RequestParam(required = false, defaultValue = "-1") String sex,
                                      @RequestParam(required = false, defaultValue = "-1") String nativepro,
                                      @RequestParam(required = false, defaultValue = "-1") String nativecity,
                                      @RequestParam(required = false, defaultValue = "-1") String nativecou,
                                      @RequestParam(required = false, defaultValue = "-1") String nativevill,
                                      @RequestParam(required = false, defaultValue = "-1") String qrbegin,
                                      @RequestParam(required = false, defaultValue = "-1") String qrend,
                                      @RequestParam(required = false, defaultValue = "-1") String zrbegin,
                                      @RequestParam(required = false, defaultValue = "-1") String zrend,
                                      @RequestParam(required = false, defaultValue = "-1") String oldKey,
                                      @RequestParam(required = false, defaultValue = "-1") String successFlag,
                                      @RequestParam(required = false, defaultValue = "-1") String uuid,
                                      @RequestParam(required = false, defaultValue = "-1") String date) {
        UserDTO user = userService.getAuthentication();
        Map<String,String> map = new HashMap<>(16);
        map.put("departid",user.getDepartid());
        map.put("pName",pName);
        map.put("idNum",idNum);
        map.put("sex",sex);
        map.put("nativepro",nativepro);
        map.put("nativecity",nativecity);
        map.put("nativecou",nativecou);
        map.put("nativevill",nativevill);
        map.put("qrbegin",qrbegin);
        map.put("qrend",qrend);
        map.put("zrbegin",zrbegin);
        map.put("zrend",zrend);
        map.put("querykey",querykey);
        map.put("oldkey",oldKey);
        map.put("successFlag",successFlag);
        map.put("uuid",uuid);
        if(!Constants.NumberStr.NUM_STR_FU_ONE.equals(date)) {
            date = date.substring(0,10).replace("-","");
        }
        map.put("date",date);
        String mapText = JsonUtil.toText(map);
        return baseInfoService.getInmatesListExcel(pageIndex,pageSize,mapText);
    }

    /**
     * 根据犯罪编号查询罪犯基本信息
     * @param prinum 犯罪编号
     * @return String
     */
    @PostMapping("/azbj/inmates/getbaseinfo")
    public String getBaseinfoToEdit(@RequestParam(required = false, defaultValue = "-1") String prinum) {
        return baseInfoService.getInmateInfo(prinum);
    }

    /**
     * 根据犯罪编号查询罪犯基本信息 - excel
     * @param prinum 犯罪编号
     * @return String
     */
    @PostMapping("/azbj/inmates/getbaseinfoexcel")
    public String getBaseinfoToEditExcel(@RequestParam(required = false, defaultValue = "-1") String prinum) {
        return baseInfoService.getInmateInfoExcel(prinum);
    }

    /**
     * 预安置管理 - 预安置人员信息录入 - 删除(逻辑删除)
     * @param prinum 犯罪编号
     * @return Result
     */
    @Log(type = "删除", value = "删除服刑人员信息")
    @PostMapping("/azbj/baseinfo/removeinfo")
    public Result removeBi(HttpServletRequest request,
                           @RequestParam(required = false, defaultValue = "-1") String prinum) {
        return baseInfoService.removeListBaseInfo(prinum);
    }

    /**
     * 批量删除服刑人员信息（逻辑删除）excel
     * @param prinumLists 罪犯编号集合
     * @return Result
     */
    @Log(type = "删除", value = "批量删除服刑人员信息")
    @PostMapping("/azbj/baseinfo/removeinfoexcels")
    public Result removeBiExcels(@RequestParam(name = "prinumLists",required = false) List<String> prinumLists) {
        return baseInfoService.removeListBaseInfoExcels(prinumLists);
    }

    /**
     * 删除服刑人员信息（逻辑删除）excel
     * @param request request
     * @param prinum prinum
     * @return Result
     */
    @Log(type = "删除", value = "删除服刑人员信息")
    @PostMapping("/azbj/baseinfo/removeinfoexcel")
    public Result removeBiExcel(HttpServletRequest request,
                                @RequestParam(required = false, defaultValue = "-1") String prinum) {
        return baseInfoService.removeListBaseInfoExcel(prinum);
    }

    /**
     * 检查身份证是否重复
     * @param priNum 服刑人员编号
     * @param idNum 身份证号
     * @return Result
     */
    @RequestMapping("/azbj/inmates/checkIdNumUpdate/{priNum}/{idNum}")
    public Result checkIdNumUpdate(
            @PathVariable(required = false) String priNum,@PathVariable(required = false) String idNum) {
        return baseInfoService.checkIdNum(priNum,idNum);
    }

    /**
     * 查询idnum的罪犯信息
     * @param idNum 用户的号码
     * @return String
     */
    @RequestMapping("/azbj/inmates/findPrisonerByIdNum")
    public String findPrisonerByIdNum(@RequestParam(value = "idNum", required = false) String idNum) {
        return baseInfoService.findPrisonerByIdNum(idNum);
    }

    /**
     * 山西个性化需求:预安置人员管理 - 全部人员管理
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param sortField sortField
     * @param sortOrder sortOrder
     * @param querykey querykey
     * @param pName pName
     * @param idNum idNum
     * @param nativepro nativepro
     * @param nativecity nativecity
     * @param nativecou nativecou
     * @param nativevill nativevill
     * @param sfDatekey sfDatekey
     * @param rjKey rjKey
     * @param ztjs ztjs
     * @return String
     */
    @RequestMapping("/azbj/Qbry/info/list")
    public String getQbryList(Integer pageIndex,Integer pageSize,
                              @RequestParam(required = false, defaultValue = "-1") String sortField,
                              @RequestParam(required = false, defaultValue = "-1") String sortOrder,
                              @RequestParam(required = false, defaultValue = "-1") String querykey,
                              @RequestParam(required = false, defaultValue = "-1") String pName,
                              @RequestParam(required = false, defaultValue = "-1") String idNum,
                              @RequestParam(required = false, defaultValue = "-1") String nativepro,
                              @RequestParam(required = false, defaultValue = "-1") String nativecity,
                              @RequestParam(required = false, defaultValue = "-1") String nativecou,
                              @RequestParam(required = false, defaultValue = "-1") String nativevill,
                              @RequestParam(required = false, defaultValue = "-1") String sfDatekey,
                              @RequestParam(required = false, defaultValue = "-1") String rjKey,
                              @RequestParam(required = false, defaultValue = "-1") String ztjs) {
        UserDTO user = userService.getAuthentication();
        Map<String,String> map = new HashMap<>(16);
        map.put("departid",user.getDepartid());
        map.put("level",user.getLevel());
        map.put("pName",pName);
        map.put("nativepro",nativepro);
        map.put("nativecity",nativecity);
        map.put("nativecou",nativecou);
        map.put("nativevill",nativevill);
        map.put("sfDateKey",sfDatekey);
        map.put("rjKey",rjKey);
        map.put("ztjs",ztjs);
        String mapText = JsonUtil.toText(map);
        return baseInfoService.getQbryList(pageIndex,pageSize,mapText,idNum,sortField,sortOrder);
    }
}